package com.jieshuibao.service;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.CounselorMapper;
import com.jieshuibao.mapper.RedEnvelopesMapper;
import com.jieshuibao.mapper.UserDealRecordExMapper;
import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.model.db.Counselor;
import com.jieshuibao.model.db.RedEnvelopes;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class UserCounselorService {

    @Resource
    private UserMapper userMapper;
    @Resource
	private CounselorMapper counselorMapper;
    @Resource
    private RedEnvelopesMapper redEnvelopesMapper;
    @Resource
    private UserDealRecordMapper userDealRecordMapper;
    @Resource
    private UserDealRecordExMapper userDealRecordExMapper;
    
    public void updateUserCounselor(User user,Counselor counselor){
    	if(user!=null){
    		
    		userMapper.updateByPrimaryKeySelective(user);
    	}
    	if(counselor!=null){
    		
    		counselorMapper.updateByPrimaryKeySelective(counselor);
    	}
    }
    public void updateUserAndAddCounselor(User user,Counselor counselor){
    	if(user!=null){
    		
    		userMapper.updateByPrimaryKeySelective(user);
    	}
    	if(counselor!=null){
    		
    		counselorMapper.insertSelective(counselor);
    	}
    }
    //领取红包业务
    public void updateAndAddRedEnvelopes(User user,RedEnvelopes redEnvelopes){
    	if(user!=null&&redEnvelopes!=null){
    		
    		userMapper.updateByPrimaryKeySelective(user);
    		redEnvelopesMapper.updateByPrimaryKeySelective(redEnvelopes);
    		//公司发红包的流水是否需要修改？
    		UserDealRecord userDealRecord1 = userDealRecordExMapper.selectUserDealRecord(user.getPhoneNumber(), redEnvelopes.getUuid());
    		if(userDealRecord1!=null){
    			userDealRecord1.setPaySucTime(new Date());
    			userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
    			userDealRecordMapper.updateByPrimaryKeySelective(userDealRecord1);
    		}
    		
    		
    		UserDealRecord userDealRecord2 = new UserDealRecord();//用户领红包流水
    		Date date = new Date();
    		
    		String uuid = UUIDGenerator.getUUID();
    		userDealRecord2.setUuid(uuid);
    		userDealRecord2.setUserId(user.getPhoneNumber());
    		userDealRecord2.setBusinessNo(redEnvelopes.getUuid());
    		userDealRecord2.setCreateDate(date);
    		userDealRecord2.setSum(redEnvelopes.getMoney());
    		userDealRecord2.setPaySucTime(date);
    		userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_ENVELOPES);
    		userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
    		userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
    		userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
    		userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
    		userDealRecordMapper.insertSelective(userDealRecord2);
    		
    	}
    	
    	
    	
    }
}
